package com.njust.config;
import com.njust.filter.CorsFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.ArrayList;
import java.util.List;


/**
 * @author zjwpro@foxmail.com
 * @date 2018/1/20 上午10:03
 */
@Configuration
public class CorsConfig extends WebMvcConfigurerAdapter {

    ////解决跨越问题
    //@Override
    //public void addCorsMappings(CorsRegistry registry) {
    //    registry.addMapping("/**")
    //            .allowedOrigins("*")
    //            .allowedMethods("PUT", "DELETE","GET","POST")
    //            .allowedHeaders("*")
    //            .exposedHeaders("access-control-allow-headers",
    //                    "access-control-allow-methods",
    //                    "access-control-allow-origin",
    //                    "access-control-max-age",
    //                    "X-Frame-Options")
    //            .allowCredentials(false).maxAge(3600);
    //}


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
               registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
                registry.addResourceHandler("/webjars/**")
       .addResourceLocations("classpath:/META-INF/resources/webjars/");
        super.addResourceHandlers(registry);
    }
    //
    //@Override
    //public void addInterceptors(InterceptorRegistry registry) {
    //    //"/user/login","/user/register"
    //    registry.addInterceptor(new LoginInterceptor()).excludePathPatterns("/**");
    //}
    //@Override
    //public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    //    FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
    //    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    //    fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
    //    //处理中文乱码问题
    //    List<MediaType> fastMediaTypes = new ArrayList<>();
    //    fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
    //    fastConverter.setSupportedMediaTypes(fastMediaTypes);
    //    fastConverter.setFastJsonConfig(fastJsonConfig);
    //    converters.add(fastConverter);
    //}


    @Bean
    public FilterRegistrationBean TestFilterRegBean(){
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        CorsFilter corsFilter = new CorsFilter();
        registrationBean.setFilter(corsFilter);
        return registrationBean;
    }
}
